package cn.HL.system.service.impl;

import cn.HL.system.domain.Role;
import cn.HL.system.mapper.RoleMapper;
import cn.HL.system.service.IRoleService;
import cn.HL.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HL
 * @since 2022-06-21
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;



    @Override
    public void save(Role role) {
        roleMapper.save(role);
        initRelationTable(role);
    }

    /**
     * 操作
     * @param role
     */
    private void initRelationTable(Role role) {
        //前端要求必须传递最新的全量数据， 我们直接做全量覆盖
        //一：删除历史数据 只用删除中间表即可
        //删除菜单
        roleMapper.deleteRoleMenuByRoleId(role.getId());
        //删除按钮
        roleMapper.deleteRolePermissionByRoleId(role.getId());
        //新增
        //二：保存最新的数据 保存中间表即可
        //1.保存角色权限表数据
        roleMapper.saveRoleMenu(role);
        //2.保存角色菜单表数据
        roleMapper.saveRolePermission(role);
    }

    @Override
    public void removeById(Long id) {
        //删除菜单
        roleMapper.deleteRoleMenuByRoleId(id);
        //删除按钮
        roleMapper.deleteRolePermissionByRoleId(id);
        roleMapper.deleteById(id);


    }

    @Override
    public void update(Role role) {
        roleMapper.update(role);
        initRelationTable(role);
    }




}
